﻿unit CmRegionModel;

interface

uses
  FastMM4,
  FastMove,
  System.SysUtils,
  System.Classes,
  System.Types,
  Winapi.Windows,
  Data.DB,
  Data.Win.ADODB,
  Datasnap.DBClient,
  Datasnap.Provider,
  {$IFDEF UNICODE}
  AnsiStrings,
  {$ENDIF }
  QBParcel,
  DllSpread,
  Generics.Collections,
  DateUtils,
  QJson,
  UserConnection,
  DBAccessor,
  DllPlugin,
  StrUtils,
  SK0001Comm;

type

  TCmRegionModel = class(TObject)
  public 
    const cModuleKey='CmRegion'; 
    const cTblName ='cm_region';
    const cBaseView = 'view_cm_region';
    const cObjNameKey='ObjName';
    const cObjGUIDKey='ObjGUID';
    const cObjItemsKey='Items';
    const cCdsModelKey='CdsModel';
    
    class function GetCountSQL(const iCondition:string; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;
    class function GetQuerySQL(const iCondition:string; const dOrderBy:string=''; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;
    class function GetPageQuerySQL(const iPageNo,iPageSize:Integer; const iCondition,iOrderBy:string; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;

    const cRegionIdKey='region_id';
    const cRegionTreeIdKey='region_tree_id';
    const cRegionTreePidKey='region_tree_pid';
    const cRegionNameKey='region_name';
    const cRegionIsSystemKey='region_is_system';
    const cRegionStatusKey='region_status';
    const cRegionSortNoKey='region_sort_no';
    const cRegionRemarkKey='region_remark';
    const cOrgIdKey='org_id';
    const cRecordPsnKey='record_psn';
    const cRecordDateKey='record_date';
    const cModifyPsnKey='modify_psn';
    const cModifyDateKey='modify_date';
    const cTimeStampKey='time_stamp';
  end;
  
  TCmRegion = class(TObject)
  private    
    fRegionId:string;
    fRegionTreeId:string;
    fRegionTreePid:string;
    fRegionName:string;
    fRegionIsSystem:Boolean;
    fRegionStatus:Integer;
    fRegionSortNo:Integer;
    fRegionRemark:string;
    fOrgId:string;
    fRecordPsn:string;
    fRecordDate:TDateTime;
    fModifyPsn:string;
    fModifyDate:TDateTime;
    fTimeStamp:Int64;
    
  protected
    fObjGUID:string;
    fInitDate:TDateTime;
    fCdsModel:TClientDataSet;
    function ObjectToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    
  public
    constructor Create();overload;
    destructor Destroy;override;
    
    procedure ResetObject;
    
    function LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
    function SaveToStr():string;
    
    property ObjGUID:string read fObjGUID;
    property RegionId:string read fRegionId write fRegionId;
    property RegionTreeId:string read fRegionTreeId write fRegionTreeId;
    property RegionTreePid:string read fRegionTreePid write fRegionTreePid;
    property RegionName:string read fRegionName write fRegionName;
    property RegionIsSystem:Boolean read fRegionIsSystem write fRegionIsSystem;
    property RegionStatus:Integer read fRegionStatus write fRegionStatus;
    property RegionSortNo:Integer read fRegionSortNo write fRegionSortNo;
    property RegionRemark:string read fRegionRemark write fRegionRemark;
    property OrgId:string read fOrgId write fOrgId;
    property RecordPsn:string read fRecordPsn write fRecordPsn;
    property RecordDate:TDateTime read fRecordDate write fRecordDate;
    property ModifyPsn:string read fModifyPsn write fModifyPsn;
    property ModifyDate:TDateTime read fModifyDate write fModifyDate;
    property TimeStamp:Int64 read fTimeStamp write fTimeStamp;
  end;

  TCmRegionList = class(TDictionary<string,TCmRegion>)
  private
    fCdsModel:TClientDataSet;
    fSortKeys:TStrings;
    
  public
    constructor Create();overload;
    destructor Destroy;override;
    
    procedure ResetObject;
    
    function LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
    function SaveToStr():string;
    
    function FindByKey(const iKey:string):TCmRegion;
    function IsExist(iKey:string):Boolean;
    function Add(const iItem:TCmRegion):string;
    function Delete(const iKey:string):Boolean;
    function Remove(const iKey:string):TCmRegion;
    procedure Clear;
    
    property SortKeys:TStrings read fSortKeys write fSortKeys;
  end;

implementation

class function TCmRegionModel.GetCountSQL(const iCondition:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TCmRegionModel.cTblName;
    dblmTable: ReadName:=TCmRegionModel.cTblName;
    dblmView: ReadName:=TCmRegionModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin
    Result:=Format(string('SELECT COUNT(*) AS ''COUNT'' FROM %s;'),[ReadName]);
  end else begin
    Result:=Format(string('SELECT COUNT(*) AS ''COUNT'' FROM %s WHERE %s;'),[ReadName,iCondition]);
  end;
end;

class function TCmRegionModel.GetQuerySQL(const iCondition:string; const dOrderBy:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TCmRegionModel.cTblName;
    dblmTable: ReadName:=TCmRegionModel.cTblName;
    dblmView: ReadName:=TCmRegionModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin
    Result:=Format(string('SELECT * FROM %s %s;'),[ReadName,
                                                 IfThen(SameStr(dOrderBy,EmptyStr),'','ORDER BY '+dOrderBy)]);
  end else begin
    Result:=Format(string('SELECT * FROM %s WHERE %s %s;'),[ReadName,iCondition,
                                                            IfThen(SameStr(dOrderBy,EmptyStr),'','ORDER BY '+dOrderBy)]);
  end;
end;

class function TCmRegionModel.GetPageQuerySQL(const iPageNo,iPageSize:Integer; const iCondition,iOrderBy:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TCmRegionModel.cTblName;
    dblmTable: ReadName:=TCmRegionModel.cTblName;
    dblmView: ReadName:=TCmRegionModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin      
    Result:=Format(string('SELECT * FROM %s'),[ReadName]);
  end else begin
    Result:=Format(string('SELECT * FROM %s WHERE %s'),[ReadName,iCondition]);
  end;
  Result:=Format(string('SELECT TOP %d * FROM (SELECT row_number() OVER(ORDER BY %s) AS _row_no_,* FROM (%s) AS A) AS B WHERE _row_no_>%d ORDER BY _row_no_'),
                  [iPageSize,
                  IfThen(SameStr(Trim(iOrderBy),EmptyStr),'',iOrderBy),
                  Result,
                  (iPageNo-1)*iPageSize]);
end;

constructor TCmRegion.Create();
begin
  inherited Create;
  fInitDate:=EncodeDateTime( 1899, 12, 30, 0, 0, 0, 0 );
  fCdsModel:=TClientDataSet.Create(nil);
  ResetObject;
end;

destructor TCmRegion.Destroy;
begin
  FreeAndNil(fCdsModel);
  inherited Destroy;
end;

procedure TCmRegion.ResetObject;
begin
  fObjGUID:=TSK0001Comm.GetGUID;
  fRegionId:=EmptyStr;
  fRegionTreeId:=EmptyStr;
  fRegionTreePid:=EmptyStr;
  fRegionName:=EmptyStr;
  fRegionIsSystem:=False;
  fRegionStatus:=-1;
  fRegionSortNo:=-1;
  fRegionRemark:=EmptyStr;
  fOrgId:=EmptyStr;
  fRecordPsn:=EmptyStr;
  fRecordDate:=fInitDate;
  fModifyPsn:=EmptyStr;
  fModifyDate:=fInitDate;
  fTimeStamp:=-1;
end;

function TCmRegion.LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    ResetObject;
    if (iCds.FindField(TCmRegionModel.cRegionIdKey)<>nil) then
    begin
      fRegionId:=iCds.FieldByName(TCmRegionModel.cRegionIdKey).AsString;     
      fObjGUID:=TSK0001Comm.SetObjGUID(fRegionId);
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRegionIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cRegionTreeIdKey)<>nil) then
    begin
      fRegionTreeId:=iCds.FieldByName(TCmRegionModel.cRegionTreeIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRegionTreeIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cRegionTreePidKey)<>nil) then
    begin
      fRegionTreePid:=iCds.FieldByName(TCmRegionModel.cRegionTreePidKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRegionTreePidKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cRegionNameKey)<>nil) then
    begin
      fRegionName:=iCds.FieldByName(TCmRegionModel.cRegionNameKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRegionNameKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cRegionIsSystemKey)<>nil) then
    begin
      fRegionIsSystem:=iCds.FieldByName(TCmRegionModel.cRegionIsSystemKey).AsBoolean;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRegionIsSystemKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cRegionStatusKey)<>nil) then
    begin
      fRegionStatus:=iCds.FieldByName(TCmRegionModel.cRegionStatusKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRegionStatusKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cRegionSortNoKey)<>nil) then
    begin
      fRegionSortNo:=iCds.FieldByName(TCmRegionModel.cRegionSortNoKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRegionSortNoKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cRegionRemarkKey)<>nil) then
    begin
      fRegionRemark:=iCds.FieldByName(TCmRegionModel.cRegionRemarkKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRegionRemarkKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cOrgIdKey)<>nil) then
    begin
      fOrgId:=iCds.FieldByName(TCmRegionModel.cOrgIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cOrgIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cRecordPsnKey)<>nil) then
    begin
      fRecordPsn:=iCds.FieldByName(TCmRegionModel.cRecordPsnKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRecordPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cRecordDateKey)<>nil) then
    begin
      if iCds.FindField(TCmRegionModel.cRecordDateKey).IsNull then
      begin
        fRecordDate:=fInitDate;
      end else begin
        fRecordDate:=iCds.FieldByName(TCmRegionModel.cRecordDateKey).AsDateTime;
      end;    
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRecordDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cModifyPsnKey)<>nil) then
    begin
      fModifyPsn:=iCds.FieldByName(TCmRegionModel.cModifyPsnKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cModifyPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cModifyDateKey)<>nil) then
    begin
      if iCds.FindField(TCmRegionModel.cModifyDateKey).IsNull then
      begin
        fModifyDate:=fInitDate;
      end else begin
        fModifyDate:=iCds.FieldByName(TCmRegionModel.cModifyDateKey).AsDateTime;
      end;    
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cModifyDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cTimeStampKey)<>nil) then
    begin
      fTimeStamp:=iCds.FieldByName(TCmRegionModel.cTimeStampKey).AsLargeInt;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cTimeStampKey]);
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadFromCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmRegion.ObjectToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    if (iCds.FindField(TCmRegionModel.cRegionIdKey)<>nil) then
    begin
      iCds.FieldByName(TCmRegionModel.cRegionIdKey).AsString:=fRegionId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRegionIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cRegionTreeIdKey)<>nil) then
    begin
      iCds.FieldByName(TCmRegionModel.cRegionTreeIdKey).AsString:=fRegionTreeId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRegionTreeIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cRegionTreePidKey)<>nil) then
    begin
      iCds.FieldByName(TCmRegionModel.cRegionTreePidKey).AsString:=fRegionTreePid;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRegionTreePidKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cRegionNameKey)<>nil) then
    begin
      iCds.FieldByName(TCmRegionModel.cRegionNameKey).AsString:=fRegionName;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRegionNameKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cRegionIsSystemKey)<>nil) then
    begin
      iCds.FieldByName(TCmRegionModel.cRegionIsSystemKey).AsBoolean:=fRegionIsSystem;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRegionIsSystemKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cRegionStatusKey)<>nil) then
    begin
      iCds.FieldByName(TCmRegionModel.cRegionStatusKey).AsInteger:=fRegionStatus;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRegionStatusKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cRegionSortNoKey)<>nil) then
    begin
      iCds.FieldByName(TCmRegionModel.cRegionSortNoKey).AsInteger:=fRegionSortNo;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRegionSortNoKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cRegionRemarkKey)<>nil) then
    begin
      iCds.FieldByName(TCmRegionModel.cRegionRemarkKey).AsString:=fRegionRemark;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRegionRemarkKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cOrgIdKey)<>nil) then
    begin
      iCds.FieldByName(TCmRegionModel.cOrgIdKey).AsString:=fOrgId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cOrgIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cRecordPsnKey)<>nil) then
    begin
      iCds.FieldByName(TCmRegionModel.cRecordPsnKey).AsString:=fRecordPsn;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRecordPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cRecordDateKey)<>nil) then
    begin
      if SameDateTime(fRecordDate,fInitDate) then
      begin
        iCds.FieldByName(TCmRegionModel.cRecordDateKey).AsString:=EmptyStr;
      end else begin
        iCds.FieldByName(TCmRegionModel.cRecordDateKey).AsDateTime:=fRecordDate;
      end;    
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cRecordDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cModifyPsnKey)<>nil) then
    begin
      iCds.FieldByName(TCmRegionModel.cModifyPsnKey).AsString:=fModifyPsn;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cModifyPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cModifyDateKey)<>nil) then
    begin
      if SameDateTime(fModifyDate,fInitDate) then
      begin
        iCds.FieldByName(TCmRegionModel.cModifyDateKey).AsString:=EmptyStr;
      end else begin
        iCds.FieldByName(TCmRegionModel.cModifyDateKey).AsDateTime:=fModifyDate;
      end;    
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cModifyDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmRegionModel.cTimeStampKey)<>nil) then
    begin
      iCds.FieldByName(TCmRegionModel.cTimeStampKey).AsLargeInt:=fTimeStamp;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmRegionModel.cTimeStampKey]);
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.ObjectToCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmRegion.SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    iCds.Append;
    if ObjectToCDS(iCds,oError) then
    begin
      iCds.Post;
    end else begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.SaveToCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmRegion.LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
var
  Json:TQJson;
  MemStream:TMemoryStream;
begin
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    if Json.TryParse(iJsonStr) then
    begin
      if (Json.ItemByName(TCmRegionModel.cObjNameKey)<>nil) then
      begin
        if (Self.ClassName<>Json.ItemByName(TCmRegionModel.cObjNameKey).AsString) then
        begin
          //oError:=Format(string('调用%s.LoadFromStr函数！错误内容:对象类名不匹配.'),[Self.ClassName]);
          //Result:=False;
          //Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性节点.'),[Self.ClassName,TCmRegionModel.cObjNameKey]);
        Result:=False;
        Exit;
      end;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:解析Json失败.iJsonStr:%s'),[Self.ClassName,iJsonStr]);
      Result:=False;
      Exit;
    end;
    ResetObject;
      
    if (Json.ItemByName(TCmRegionModel.cCdsModelKey)<>nil) and 
      not SameStr(Trim(Json.ItemByName(TCmRegionModel.cCdsModelKey).AsString),EmptyStr) then
    begin
      TSK0001Comm.Base64StrToMemStream(AnsiString(Json.ItemByName(TCmRegionModel.cCdsModelKey).AsString),MemStream);
      if MemStream.Size>0 then
      begin
        fCdsModel.LoadFromStream(MemStream);
      end;
    end;
    
    if (Json.ItemByName(TCmRegionModel.cObjGUIDKey)<>nil) and 
      not SameStr(Trim(Json.ItemByName(TCmRegionModel.cObjGUIDKey).AsString),EmptyStr) then
    begin
      fObjGUID:=Json.ItemByName(TCmRegionModel.cObjGUIDKey).AsString;
    end;
    
    if Json.ItemByName('RegionId')<>nil then
    begin
      fRegionId:=Json.ItemByName('RegionId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RegionId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RegionTreeId')<>nil then
    begin
      fRegionTreeId:=Json.ItemByName('RegionTreeId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RegionTreeId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RegionTreePid')<>nil then
    begin
      fRegionTreePid:=Json.ItemByName('RegionTreePid').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RegionTreePid']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RegionName')<>nil then
    begin
      fRegionName:=Json.ItemByName('RegionName').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RegionName']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RegionIsSystem')<>nil then
    begin
      fRegionIsSystem:=Json.ItemByName('RegionIsSystem').AsBoolean;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RegionIsSystem']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RegionStatus')<>nil then
    begin
      fRegionStatus:=Json.ItemByName('RegionStatus').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RegionStatus']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RegionSortNo')<>nil then
    begin
      fRegionSortNo:=Json.ItemByName('RegionSortNo').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RegionSortNo']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RegionRemark')<>nil then
    begin
      fRegionRemark:=Json.ItemByName('RegionRemark').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RegionRemark']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('OrgId')<>nil then
    begin
      fOrgId:=Json.ItemByName('OrgId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'OrgId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RecordPsn')<>nil then
    begin
      fRecordPsn:=Json.ItemByName('RecordPsn').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RecordPsn']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RecordDate')<>nil then
    begin
      fRecordDate:=Json.ItemByName('RecordDate').AsDateTime;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RecordDate']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ModifyPsn')<>nil then
    begin
      fModifyPsn:=Json.ItemByName('ModifyPsn').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ModifyPsn']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ModifyDate')<>nil then
    begin
      fModifyDate:=Json.ItemByName('ModifyDate').AsDateTime;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ModifyDate']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('TimeStamp')<>nil then
    begin
      fTimeStamp:=Json.ItemByName('TimeStamp').AsInt64;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'TimeStamp']);
      Result:=False;
      Exit;  
    end;
    
  finally
    Json.Free;
    MemStream.Free;
  end;
  Result:=True;
end;

function TCmRegion.SaveToStr():string;
var
  Json:TQJson;
  MemStream:TMemoryStream;
begin
  Result:=EmptyStr;  
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create; 
  try
    Json.Add('ModuleKey').AsString:=TCmRegionModel.cModuleKey;
    Json.Add(TCmRegionModel.cObjNameKey).AsString:=Self.ClassName;
    
    fCdsModel.SaveToStream(MemStream);
    Json.Add(TCmRegionModel.cCdsModelKey).AsString:=string(TSK0001Comm.MemStreamToBase64Str(MemStream));
    
    Json.Add(TCmRegionModel.cObjGUIDKey).AsString:=fObjGUID;
    Json.Add('RegionId').AsString:=fRegionId;
    Json.Add('RegionTreeId').AsString:=fRegionTreeId;
    Json.Add('RegionTreePid').AsString:=fRegionTreePid;
    Json.Add('RegionName').AsString:=fRegionName;
    Json.Add('RegionIsSystem').AsBoolean:=fRegionIsSystem;
    Json.Add('RegionStatus').AsInteger:=fRegionStatus;
    Json.Add('RegionSortNo').AsInteger:=fRegionSortNo;
    Json.Add('RegionRemark').AsString:=fRegionRemark;
    Json.Add('OrgId').AsString:=fOrgId;
    Json.Add('RecordPsn').AsString:=fRecordPsn;
    Json.Add('RecordDate').AsDateTime:=fRecordDate;
    Json.Add('ModifyPsn').AsString:=fModifyPsn;
    Json.Add('ModifyDate').AsDateTime:=fModifyDate;
    Json.Add('TimeStamp').AsInt64:=fTimeStamp;
    Result:=Json.AsString;
  finally
    Json.Free;
    MemStream.Free;
  end;
end;

constructor TCmRegionList.Create();
begin
  inherited Create;
  fCdsModel:=TClientDataSet.Create(nil);
  fSortKeys:=TStringList.Create;
  ResetObject;
end;

destructor TCmRegionList.Destroy;
begin
  FreeAndNil(fCdsModel);
  Clear;
  FreeAndNil(fSortKeys);
  inherited Destroy;
end;

procedure TCmRegionList.ResetObject;
begin
  Clear;
end;

function TCmRegionList.LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
var
  Item:TCmRegion;
begin
  ResetObject;
  if iCds<>nil then
  begin
    iCds.First;
    while not iCds.Eof do
    begin
      Item:=TCmRegion.Create;
      if Item.LoadFromCDS(iCds,oError) then
      begin
        if Add(Item)=EmptyStr then
        begin
          Item.Free;
          Result:=False;
          Exit;
        end;
      end else begin
        Result:=False;
        Exit;
      end;
      iCds.Next;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadFromCDS函数参数错误! 错误内容传入的数据集为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmRegionList.SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
var
  Key:string;
begin
  if (iCds<>nil) then
  begin
  for Key in fSortKeys do
  begin
    iCds.Append;
    if Items[Key].ObjectToCDS(iCds,oError) then
    begin
      iCds.Post;
    end else begin
      Result:=False;
      Exit;
    end;
  end;
  end else begin
    oError:=Format(string('调用%s.SaveToCDS函数参数错误! 错误内容:传入的数据集为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmRegionList.LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
var
  Json,ItemJson,ArrayJson:TQJson;
  Item:TCmRegion;
  MemStream:TMemoryStream;
begin
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    if Json.TryParse(iJsonStr) then
    begin
      if (Json.ItemByName(TCmRegionModel.cObjNameKey)<>nil) then
      begin
        if (Self.ClassName<>Json.ItemByName(TCmRegionModel.cObjNameKey).AsString) then
        begin
          //oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:对象类名不匹配.'),[Self.ClassName]);
          //Result:=False;
          //Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:缺少%s属性节点.'),[Self.ClassName,TCmRegionModel.cObjNameKey]);
        Result:=False;
        Exit;
      end;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:解析Json失败.iJsonStr:%s'),[Self.ClassName,iJsonStr]);
      Result:=False;
      Exit;
    end;
    if (Json.ItemByName(TCmRegionModel.cObjItemsKey)<>nil) and
      (Json.ItemByName(TCmRegionModel.cObjItemsKey).IsArray) then
    begin
      ResetObject;
      if Json.ItemByName(TCmRegionModel.cCdsModelKey)<>nil then
      begin
        TSK0001Comm.Base64StrToMemStream(AnsiString(Json.ItemByName(TCmRegionModel.cCdsModelKey).AsString),MemStream);
        if MemStream.Size>0 then
        begin
          fCdsModel.LoadFromStream(MemStream);
        end;
      end;
      ArrayJson:=Json.ItemByName(TCmRegionModel.cObjItemsKey);
      for ItemJson in ArrayJson do
      begin
        Item:=TCmRegion.Create;
        Item.LoadFromStr(ItemJson.AsString,oError);
        if Add(Item)=EmptyStr then
        begin
          Item.Free;
          Result:=False;
          Exit;
        end;
      end;
    end else begin
      Result:=False;
      Exit;
    end;
  finally
    Json.Free;
    MemStream.Free;
  end;
  Result:=True;
end;

function TCmRegionList.SaveToStr():string;
var
  Key:string;
  Json,ItemJson,ArrayJson:TQJson;
  MemStream:TMemoryStream;
begin
  Result:=EmptyStr;
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    Json.Add(TCmRegionModel.cObjNameKey).AsString:=Self.ClassName;
    fCdsModel.SaveToStream(MemStream);
    Json.Add(TCmRegionModel.cCdsModelKey).AsString:=string(TSK0001Comm.MemStreamToBase64Str(MemStream));

    ArrayJson:=Json.Add(TCmRegionModel.cObjItemsKey,jdtArray);
    for Key in fSortKeys do
    begin
      ItemJson:=TQJson.Create;
      if ItemJson.TryParse(Items[Key].SaveToStr()) then
      begin
        ArrayJson.Add(ItemJson);
      end else begin
        ItemJson.Free;
      end;
    end;
    Result:=Json.AsString;
  finally
    Json.Free;
    MemStream.Free;
  end;
end;

function TCmRegionList.FindByKey(const iKey:string):TCmRegion;
var
  Item:TCmRegion;
begin
  if TryGetValue(iKey,Item) then
  begin
    Result:=Item;
  end else begin
    Result:=nil;
  end;
end;

function TCmRegionList.IsExist(iKey:string):Boolean;
var
  Item:TCmRegion;
begin
  Result:= TryGetValue(iKey,Item);
end;

function TCmRegionList.Add(const iItem:TCmRegion):string;
begin
  if iItem.ObjGUID<>EmptyStr then
  begin
    inherited Add(iItem.ObjGUID,iItem);
    Result:=iItem.ObjGUID;
    fSortKeys.Add(iItem.ObjGUID);
  end else begin
    Result:=EmptyStr;
  end;
end;

function TCmRegionList.Delete(const iKey:string):Boolean;
var
  Ops:Integer;
begin
  if ContainsKey(iKey) then
  begin
    Remove(iKey).Free;
    Ops:=fSortKeys.IndexOf(iKey);
    if Ops<>-1 then
    begin
      fSortKeys.Delete(Ops);
    end;
    Result:=True;
  end else begin
    Result:=False;
  end;
end;

function TCmRegionList.Remove(const iKey:string):TCmRegion;
var
  Ops:Integer;
begin
  if ContainsKey(iKey) then
  begin
    Result:=Items[iKey];
    Ops:=fSortKeys.IndexOf(iKey);
    if Ops<>-1 then
    begin
      fSortKeys.Delete(Ops);
    end;
    inherited Remove(iKey);
  end else begin
    Result:=nil;
  end;
end;

procedure TCmRegionList.Clear;
var
  Key:string;
begin
  for Key in Keys do
  begin
    if Items[Key]<>nil then
    begin
      Items[Key].Free;
    end;
  end;
  fSortKeys.Clear;
  inherited Clear;
end;

end.
